package main

import (
	"flag"
	"fmt"
	"net"
	"os"
	"os/exec"
	"time"
)

func loopScan(port string, openPort string) {
	i := 0
	ticker := time.NewTicker(5 * time.Second)

	for _ = range ticker.C {
		i++
		if i == 30 {
			os.Exit(0)
		}

		conn, err := net.Dial("tcp", "127.0.0.1:"+port)
		if err != nil {
			fmt.Println("服务器启动中，请稍等！")
		} else {
			conn.Close()
			fmt.Println("服务器成功启动")
			cmd := exec.Command("cmd", "/c", "start", "http://127.0.0.1:"+openPort)
			cmd.Run()
			os.Exit(0)
		}
	}
}

func main() {
	var port string
	var openPort string

	flag.StringVar(&port, "p", "8080", "扫描端口号")
	flag.StringVar(&openPort, "o", "8080", "打开浏览器端口号")
	flag.Parse()

	fmt.Printf("port：%v\nopenPort：%v\n", port, openPort)
	loopScan("8080", "8080")
}
